﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using LDataAccess;
using LDriver;
using LModels.Base;
using Proxy = LModels.Proxy.Proxy;

namespace Models
{
    public class Survey : BaseModel
    {

        public ISurvey survey { get; private set; }

        private long IDSurvey { get; set; }
        private string Value { get; set; }
        private DateTime Date { get; set; }
        private long IDRecording { get; set; }
        
        private Recording recording;
        public Recording SurveyRecording
        {
            get
            {
                if (recording == null)
                {
                    recording = new Factory<Recording>(px).Load(IDRecording) as Recording;
                    return recording;
                }
                return null;
            }
        }

        public Survey()
        {
        }

        public Survey(Proxy pxProxy) : base(pxProxy)
        {
        }

        public Survey(ISurvey s, long idRecording)
        {
            survey = s;

            Value = survey.ToString();
            Date = DateTime.Now;
            IDRecording = idRecording;
        }


        protected internal override BaseModel Load(long IDSurvey)
        {
            var reader = px.Survey.Load(IDSurvey);
            if (reader.Read())
            {

                SetPK((long)reader["IDSurvey"]);

                Value = reader["Value"] as string;
                Date = reader["DateTime"] is DateTime ? (DateTime) reader["DateTime"] : new DateTime();
                IDRecording = reader["IDRecording"] is long ? (long)reader["IDRecording"] : -1;

            }

            return this;

        }

    }

    namespace Collections
    {
        public class SurveysCollection: BaseCollection
        {
            public SurveysCollection(){}

            public SurveysCollection(Proxy pxProxy) : base(pxProxy) { }

            public SurveysCollection List()
            {
                SurveysCollection ret = new SurveysCollection();
                IDataReader dr = px.Survey.List();

                while (dr.Read())
                    ret.Add(new Factory<Survey>(px).Load((long) dr["IDSurvey"]) as Survey);

                return ret;

            }

            public SurveysCollection ListByRecording(long IDRecording)
            {
                SurveysCollection ret = new SurveysCollection();
                IDataReader dr = px.Survey.ListByRecording(IDRecording);

                while (dr.Read())
                    ret.Add(new Factory<Survey>(px).Load((long) dr["IDSurvey"]) as Survey);

                return ret;
            }

           
        }

    }

}
